package com.myapplication.ui.listfragment.shoplistactivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.myapplication.R;
import com.myapplication.bean.ShopListBean;
import com.myapplication.ui.infofragment.infowebactivity.InfoWebActivity;

import java.util.List;

/**
 * Created by My on 2016/10/15.
 */
public class ShopLvFragmentAdapter extends BaseAdapter {
    private Context context;
    private List<ShopListBean.DataBean.ItemsBean> shoplistitembean;


    public ShopLvFragmentAdapter(Context context,List<ShopListBean.DataBean.ItemsBean> shoplistitembean){
        this.context=context;
        this.shoplistitembean=shoplistitembean;
    }


    @Override
    public int getCount() {
        return shoplistitembean.size();
    }

    @Override
    public Object getItem(int i) {
        return shoplistitembean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    ViewHolder holder;
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        holder=new ViewHolder();
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_lv_shoplist,null);
            holder.iv_lv_img_url= (ImageView) view.findViewById(R.id.iv_lv_img_url);
            holder.tv_lv_itemstitle= (TextView) view.findViewById(R.id.tv_lv_itemstitle);
            holder.tv_shoplist_intro= (TextView) view.findViewById(R.id.tv_shoplist_intro);
            holder.tv_shoplist_price= (TextView) view.findViewById(R.id.tv_shoplist_price);
            holder.tv_shoplist_domain= (TextView) view.findViewById(R.id.tv_shoplist_domain);
            holder.rl_jump= (RelativeLayout) view.findViewById(R.id.rl_jump);
            holder.tv_shoplist_price_unit= (TextView) view.findViewById(R.id.tv_shoplist_price_unit);
            holder.rl_sc1= (RelativeLayout) view.findViewById(R.id.rl_sc1);
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }
        String iv_lv_img_url=shoplistitembean.get(i).getImgUrl();
        Glide.with(context).load(iv_lv_img_url).into(holder.iv_lv_img_url);
        holder.tv_lv_itemstitle.setText(shoplistitembean.get(i).getTitle());
        holder.tv_shoplist_intro.setText(shoplistitembean.get(i).getIntro());
        holder.tv_shoplist_price.setText(shoplistitembean.get(i).getPrice()+"");
        holder.tv_shoplist_price_unit.setText(shoplistitembean.get(i).getPriceUnit());
        holder.tv_shoplist_domain.setText(shoplistitembean.get(i).getDomain());
        holder.rl_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.rl_jump:
                        Intent intent=new Intent(context,InfoWebActivity.class);
                        intent.putExtra("url",shoplistitembean.get(i).getCpsUrl());
                        intent.putExtra("title",shoplistitembean.get(i).getTitle());
                        context.startActivity(intent);
                        break;
                    case R.id.rl_sc1:
                        Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                        break;
                }

            }
        });

        return view;
    }
    static class ViewHolder{
        ImageView iv_lv_img_url;
        RelativeLayout rl_jump,rl_sc1;
        TextView tv_lv_itemstitle,tv_shoplist_intro,tv_shoplist_price,tv_shoplist_domain,tv_shoplist_price_unit;
    }

}
